package com.Sing.Controller;

import com.Sing.Pojo.Admin;
import com.Sing.Service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;

@Controller
public class AdminController {
    private AdminService adminService;
    @Autowired
    @Qualifier("adminServiceImpl")
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    // 跳转到管理员管理的界面
    @RequestMapping("/adminIndex")
    public String adminIndexJump(){

        return "admin/adminIndex";
    }

    // 获取 admin 表中数据，并返还给 管理员管理 的界面
    @RequestMapping("/adminAll")
    @ResponseBody
    public HashMap<String,Object> adminAll( String username, String adminType ){

        int adminTypeTemp = 2;
        if ( username == null || username.equals( "" ) ){
            username = "%%";
        } else {
            username = "%" + username + "%";
        }

        if ( ! ( adminType == null || adminType.equals("") ) ) {
            adminTypeTemp = Integer.parseInt( adminType );
        }

        ArrayList<Admin> adminList = adminService.getAllDataByCheck(username, adminTypeTemp);

        int count = adminService.getAllDataContext( username, adminTypeTemp );
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put( "count", count );
        paramMap.put( "data", adminList );
        paramMap.put( "code", 0 );

        return paramMap;
    }

    // 删除管理员表中的数据
    @RequestMapping("/deleteAdminByIds")
    @ResponseBody
    public HashMap<String,Object> deleteAdminByIds(String ids){
        HashMap<String, Object> resultMap = new HashMap<>();

        String[] deleteIds = ids.split(",");
        for ( String id : deleteIds ){
            boolean flag = adminService.deleteDataById( Integer.parseInt( id ) );

            resultMap.put( "code", 0 );
        }

        return resultMap;
    }

    // 跳转到修改密码的页面
    @RequestMapping("/queryAdminById")
    public String queryAdminById( String id, Model model ){

        model.addAttribute( "id", id );

        return "admin/updateAdmin";
    }

    // 执行修改管理员账户的密码
    @RequestMapping("/updatePwdSubmit")
    @ResponseBody
    public HashMap<String,Object> updatePwd( String id, String oldPwd, String newPwd ){
        HashMap<String, Object> resultMap = new HashMap<>();

        // 获取待修改的 Admin 数据
        int Id = Integer.parseInt(id);
        Admin adminTemp = adminService.getDataByID( Id );

        // 进行 旧密码 的验证, 并进行 密码的修改
        String oldPassword = adminTemp.getPassword();
        if ( oldPassword.equals( oldPwd ) ){
            boolean flag = adminService.modifyPWDById(newPwd, Id);

            resultMap.put( "code", 0 );
        } else {
            resultMap.put( "msg", "修改失败" );
        }

        return resultMap;
    }

    // 跳转到 添加公告 的页面
    @RequestMapping("/adminAdd")
    public String adminAddJump(){

        return "admin/adminAdd";
    }

    // 执行添加公告的操作
    @RequestMapping("/addAdminSubmit")
    @ResponseBody
    public HashMap<String,Object> addAdminSubmit( String username, String password, String adminType ){

        HashMap<String, Object> resultMap = new HashMap<>();
        HashMap<String, Object> paramMap = new HashMap<>();

        int adminTypeTemp = Integer.parseInt(adminType);

        paramMap.put("username", username );
        paramMap.put("password", password );
        paramMap.put("adminType", adminTypeTemp );

        boolean flag = adminService.addAdmin( paramMap );

        if ( flag ){
            resultMap.put( "code", 0 );
        } else {
            resultMap.put( "msg", "添加失败" );
        }

        return resultMap;
    }
}
